/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fanta.framework.dao;

import fanta.framework.manager.ConnectionManager;
import fanta.framework.persistence.Utente;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.EntityManager;
import org.apache.log4j.Logger;

/* *
 *
 * @author alessandro De Troia
 */
public class UtenteDAO {
    private static final Logger logger = Logger.getLogger(UtenteDAO.class);
    public static Utente creaUtente(String user, String password, boolean admin, ConnectionManager cm) {
        EntityManager entityManager = (EntityManager) cm.getEntityManager();
        Utente ut = new Utente();
        try {            
            entityManager.getTransaction().begin();
            logger.debug("Start Transaction");
            ut.setAdmin(admin);
            ut.setUser(user);
            ut.setPasswordcifrata(password);
            entityManager.persist(ut);
            logger.debug("Transaction persisted");
            entityManager.getTransaction().commit();
            logger.debug("Transaction committed");
            logger.info("New utente:"+ut);

        } catch (Exception e) {
            logger.error("Errore in UtenteDAO", e);
            logger.debug("##EXCEPTION## Transaction will be roll back");
            entityManager.getTransaction().rollback();
            logger.debug("Transaction rolled back!");
        }
        return ut;
    }
    public static Utente cercaUtenteByUser(String user, ConnectionManager cm) {
        EntityManager entityManager = (EntityManager) cm.getEntityManager();
        Utente ut = new Utente();
        try {
            Object obj = entityManager.find(Utente.class, user);
            if(obj != null) {
                ut = (Utente)obj;
            }
        }catch (Exception e) {
            logger.error("Errore in UtenteDAO", e);
        }
        return ut;
    }

    public static boolean aggiornaUtente(Utente utente, ConnectionManager cm) {
        boolean result = false;
            EntityManager entityManager = (EntityManager) cm.getEntityManager();
        try {
            entityManager.getTransaction().begin();
            logger.debug("Start Transaction");
            entityManager.persist(entityManager.merge(utente));
            logger.debug("Transaction persisted");
            entityManager.getTransaction().commit();
            logger.debug("Transaction committed");
            result = true;
        } catch (Exception e) {
            logger.error("Errore in aggiornaUtenteDAO", e);
            logger.debug("##EXCEPTION## Transaction will be roll back");
            entityManager.getTransaction().rollback();
            logger.debug("Transaction rolled back!");
        }
        return result;
    }
    public static boolean cancellaUtente(String user, ConnectionManager cm) {
        boolean result = false;
        EntityManager entityManager = (EntityManager) cm.getEntityManager();
        try{
            entityManager.getTransaction().begin();
            logger.debug("Start Transaction");
            entityManager.remove(entityManager.find(Utente.class, user));
            entityManager.getTransaction().commit();
            logger.debug("Transaction committed");
            result = true;
        } catch (Exception e) {
            logger.error("Errore in cancellaUtenteDAO", e);
            logger.debug("##EXCEPTION## Transaction will be roll back");
            entityManager.getTransaction().rollback();
            logger.debug("Transaction rolled back!");
        }

        return result;
    }
    public static Collection getListaUtenti(ConnectionManager cm) {
        Collection listaUtenti = new ArrayList();
        try {
            EntityManager entityManager = (EntityManager) cm.getEntityManager();
            listaUtenti = entityManager.createNamedQuery("Utente.findAll").getResultList();
        } catch (Exception e) {
            logger.error("#EXCEPTION# in getListaUtenti!", e);
        }
        return listaUtenti;
    }
}
